<?php
	class Group_user extends MY_Controller {
		function Group_user() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();	
			$_SESSION['redirect'] = $this->uri->uri_string();
			$this->get_link();
			//kiem tra quyen truy cap 
			$this->check_permission();	
		}
		
		function index(){	
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Nhóm người dùng";
			$data['table'] = 'group_user';
			$data['controller'] = 'group_user';
			$this->db->order_by('parent_id','asc');
			$data['content_admin'] = 'back_end/group_user/index';
			$data['sort'] = 'ASC';
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
					
			$per_page = 20;
			$uri = 4;
			$data['group_user_value'] = $this->mod_lib->load_all('group_user','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/group_user/index/');
			$config['total_rows']   =  $this->db->count_all('group_user');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);		
		}
		
		function sort($field,$sort = '') {
			$per_page = 10;
			$uri = 4;
			$data['title'] = "Nhóm người dùng - Sort";
			$data['table'] = 'group_user';
			$data['controller'] = 'group_user';
			$data['content_admin'] = 'back_end/group_user/index';
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$uri_value_4 =  $this->uri->segment(4);
			$uri_value_5 =  $this->uri->segment(5);
			
			$_SESSION['order_sort'] = array(
				$uri_value_4 => $uri_value_5 
			);
			
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('group_user','',array(),array(),array("id"=>"desc"));
			$data['group_user_value'] = $this->mod_lib->load_search('group_user','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));		
			$config['base_url']     =  site_url('back_end/group_user/index');

			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);
		}
		
		function add(){	
			$data['title'] = "Thêm - nhóm người dùng";
			$data['table'] = 'group_user';
			$data['controller'] = 'group_user';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/group_user/add';
			$add = $this->input->post('addform');
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($add)){ 
				$data_value = array (
						'name'       => trim($this->input->post('name')),
						'description'       => trim($this->input->post('description')),
						'status'          => $this->input->post('status') 				
					);

				if($this->mod_lib->insert('group_user',$data_value)) {
					$this->session->set_flashdata('msview', 'Thêm thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
				}
				
				redirect('back_end/group_user');	
			} else {
				$this->load->view('back_end/template_backend',$data);
			}		
		}
		
		function edit($id = null){
			$data['title']			= "Sửa - nhóm người dùng";
			$data['content_admin'] = 'back_end/group_user/edit';
			$data['group_user_value'] = $this->db->get_where('group_user',array( 'id' => $id ))->result_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($edit)) {
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC'
			  		);		
				$data_value = array(
						'name' => trim($this->input->post('name')),
						'description' => $this->input->post('description'),
						'status' => trim($this->input->post('status')),
					);
					
				$this->mod_lib->update('group_user',$where,$order,$data_value);
				
				redirect('back_end/group_user');
			} else {
				$this->load->view('back_end/template_backend',$data);	
			}
		}
		 
		function delete(){
			$value_id_arr    = $this->input->post('value_id');
			
			if(!empty($value_id_arr)){
				$value_id_string = implode(',',$value_id_arr);
				
				if($this->mod_lib->deleteRecord('group_user',$value_id_string)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}
			
			redirect('back_end/group_user');
		}
		
		function deleteId($id) {
			if(!empty($id)){
				if($this->mod_lib->deleteRecord('group_user',$id)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}		
			redirect('back_end/admin_log');
		}
	}
?>